cd ${work}5_reg_analysis

local ylist		"log_quantity log_price"
local felist	`" "IDY FD PY" "IDY FPD FPY" "'
local wlist		`" "c.sales##c.sales##c.sales##c.sales" "" "'
local dumlist	"dum_bec_group"

use temp\regsample_ipdt, clear

eststo clear
local mtitles
local n=1
foreach y of local ylist {

local nfe=1	
foreach fe of local felist {
local w : word `nfe' of `wlist'

local estlist_ols
local estlist_iv

local m=1
foreach x of local dumlist {
	
foreach l of numlist 1 0 {
	cap gen x`l'_ols_`m' = treated_2 * (`x'==`l') if `x'!=.
	cap gen x`l'_iv_`m' = treated_2 * (`x'==`l') if `x'!=.
	gen z`l' = tdcinitiative_2 * (`x'==`l') if `x'!=.
}	
foreach l in ols iv {
	lab var x1_`l'_`m' "Non-consumption $ \times \overline{support}_{fdt} $"
	lab var x0_`l'_`m' "Consumption $ \times \overline{support}_{fdt} $"
}

	eststo est_ols_`m' : reghdfe `y' x1_ols_`m' x0_ols_`m' `w', a(`fe') vce(cl F) nosamp	
	local N = e(N)
	local estlist_ols `estlist_ols' est_ols_`m'
	
	eststo est_iv_`m' : ivreghdfe `y' ( x1_iv_`m' x0_iv_`m' = z1 z0 ) `w', a(`fe') cl(F) ffirst
	drop z1 z0
	local estlist_iv `estlist_iv' est_iv_`m'
	
	matrix first = e(first)
	local F1_`m' = first[8,1]
	local F0_`m' = first[8,2]
	matrix drop first
	
	local ++m
	
}

	eststo stacked_`n' : appendmodels `estlist_ols' `estlist_iv'
	qui estadd scalar N `N'
local m=1	
foreach x of local dumlist {
	qui estadd scalar F1_`m' `F1_`m''
	qui estadd scalar F0_`m' `F0_`m''
	local ++m
}
	qui estadd local FE `fe'
	if "`w'"!=""	qui estadd local SC "Yes"
	if "`w'"==""	qui estadd local SC "No"
	local mtitles `mtitles' "`:var lab `y''"
		
	eststo drop est*
	local ++n
	local ++nfe
	
}	
}
	
esttab using output\table_a8.tex, replace booktabs se label ///
	title("Effect of Export Support on Trade across BEC Classification of Products") star(* 0.1 ** 0.05 *** 0.01) mtitles(`mtitles') keep(x?_*) ///
	stats(N F1_1 F0_1 FE SC , labels("Observations" "Partial IV F-Stat, Non-cons." "Partial IV F-Stat, Cons." "Fixed Effects" "Size Control") fmt(%10.0fc %10.0fc %10.0fc "" "")) refcat(x1_ols_1 "OLS" x1_iv_1 "2SLS" , nolabel)